/**
 * $Id: CommonUtils.java Feb 10, 2015 2:21:35 PM hdp
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aurora.base.util;

import java.net.InetAddress;
import java.util.UUID;

/**
 * 公共工具类
 * <p>创建时间: Feb 10, 2015 2:21:35 PM</p>
 * @author <a href="mailto:hongdanping@163.com">hdp</a>
 * @version V1.0
 * @since V1.0
 */
public final class CommonUtils {
	
	public static final String defaultPath = "static/";
	/**
	 * 当前服务器ip地址
	 */
	private static final int IP;
	
	static {
		int ipadd;
		try {
			ipadd = toInt(InetAddress.getLocalHost().getAddress());
		} catch (Exception e) {
			ipadd = 0;
		}
		IP = ipadd;
	}
	/**
	 * 计数器
	 */
	private static short counter = (short) 0;
	/**
	 * JVM
	 */
	private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
	/**
	 * 获取hash路径
	 * @param uuid uuid
	 * @return
	 */
	public static String getHashPath(String uuid) {
		int hashCode = uuid.hashCode();
		StringBuilder sb = new StringBuilder(defaultPath);
		String tmp = format(hashCode);
		for (int i = 0; i < 4; i++) {
			sb.append(tmp.substring(i * 2, (i + 1) * 2)).append("/");
		}
		return sb.toString();
	}
	/**
	 * 获取随机uuid，不包含“-”符号的
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}
	
	/**
	 * 产生一个32位的UUID
	 * @return
	 */
	public static String generate() {
		return new StringBuilder(32).append(format(getIP())).append(
				format(getJVM())).append(format(getHiTime())).append(
				format(getLoTime())).append(format(getCount())).toString();
	}

	/**
	 * 将整型数据格式化为8位的16进制字符串
	 * @param intval 整型
	 * @return 8位的16进制字符串
	 */
	private final static String format(int intval) {
		String formatted = Integer.toHexString(intval);
		StringBuilder buf = new StringBuilder("00000000");
		buf.replace(8 - formatted.length(), 8, formatted);
		return buf.toString();
	}
	/**
	 * 将整型数据格式化为4位的16进制字符串
	 * @param shortval 短整型
	 * @return 4位的16进制字符串
	 */
	private final static String format(short shortval) {
		String formatted = Integer.toHexString(shortval);
		StringBuilder buf = new StringBuilder("0000");
		buf.replace(4 - formatted.length(), 4, formatted);
		return buf.toString();
	}

	/**
	 * 获取JVM
	 * @return
	 */
	private final static int getJVM() {
		return JVM;
	}
	
	/**
	 * 获取计数器
	 * @return
	 */
	private final static short getCount() {
		synchronized (CommonUtils.class) {
			if (counter < 0)
				counter = 0;
			return counter++;
		}
	}
	
	/**
	 * 获取当前服务器ip地址
	 * @return
	 */
	private final static int getIP() {
		return IP;
	}

	/**
	 * Unique down to millisecond
	 */
	private final static short getHiTime() {
		return (short) (System.currentTimeMillis() >>> 32);
	}

	private final static int getLoTime() {
		return (int) System.currentTimeMillis();
	}

	/**
	 * 字节转为整型数据
	 * @param bytes 字节数组
	 * @return
	 */
	private final static int toInt(byte[] bytes) {
		int result = 0;
		for (int i = 0; i< 4; i++) {
			result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
		}
		return result;
	}
}
